#define _CRT_SECURE_NO_WARNINGS 1

//https://leetcode.cn/problems/count-and-say/

class Solution {
public:
    string countAndSay(int n) {
        string ret = "1";
        for (int i = 1; i < n; i++)
        {
            int left = 0, right = 0, count = 0;
            string newS = "";
            while (right < ret.size())
            {

                if (right < ret.size() && ret[left] == ret[right])
                {
                    count = right - left + 1;
                    right++;
                }

                if (right >= ret.size() || ret[left] != ret[right])
                {
                    newS += to_string(count);
                    newS += ret[left];
                    left = right;
                }
            }
            ret = newS;
        }

        return ret;
    }
};